Principles of Data Visualization and Introduction to ggplot2
I have provided you with data about the 5,000 fastest growing companies in the US, as compiled by Inc. magazine. lets read this in:
library(ggplot2)
library(tidyr)
library(dplyr)
library(plotly)
inc <- read.csv("https://raw.githubusercontent.com/charleyferrari/CUNY_DATA_608/master/module1/Data/inc5000_data.csv",
header= TRUE)
And lets preview this data:
head(inc)
summary(inc)
Think a bit on what these summaries mean. Use the space below to add some more relevant non-visual exploratory information you think helps you understand this data:
count(inc, Industry)
count(inc, State)
Sys.setenv("plotly_username"="AsherMeyers")
Sys.setenv("plotly_api_key"="18paMp1dQ1Mp9itZ3R7R")
Create a graph that shows the distribution of companies in the dataset by State (ie how many are in each state). There are a lot of States, so consider which axis you should use. This visualization is ultimately going to be consumed on a 'portrait' oriented screen (ie taller than wide), which should further guide your layout choices.
ftable = table(inc$State)
ftable = ftable[order(ftable)]
ftable
ftdf <- data.frame(ftable)
colnames(ftdf) <- c("State", "Frequency")
p <- plot_ly(ftdf, x = ~Frequency, y = ~State, color = I("gray80")) %>%
add_markers(color = I("Navy")) %>%
layout(
title = "# of 5,000 Fastest Growing Firms, by State",
xaxis = list(title = "Number of Firms (logscale)"),
margin = list(l = 40)
)
p <- layout(p, xaxis = list(type = "log"), yaxis = list(dtick = 1.95))
p
#Source: https://plot.ly/r/dumbbell-plots/
# treemap
#install.packages('treemap', repos = "http://cran.us.r-project.org")
library(treemap)
ftdf$Display <- paste0(ftdf$State, ": ", as.character(ftdf$Frequency))
treemap(ftdf,
index="Display",
vSize="Frequency",
type="index",
title = "5,000 Fastest Growing Firms by State",
palette = "Set3")
Lets dig in on the state with the 3rd most companies in the data set. Imagine you work for the state and are interested in how many people are employed by companies in different industries. Create a plot that shows the average and/or median employment by industry for companies in this state (only use cases with full data, use R's complete.cases() function.) In addition to this, your graph should show how variable the ranges are, and you should deal with outliers.
#install.packages("plotly", repos = "http://cran.us.r-project.org")
incNY <- inc %>%
filter(complete.cases(.)) %>%
subset(State == "NY") %>%
group_by(Industry) %>%
summarise(low = min(Employees), firstQ = quantile(Employees, 0.25),
median = median(Employees), thirdQ = quantile(Employees, 0.75),
high = max(Employees))
incNY$Industry <- factor(incNY$Industry, levels = incNY$Industry[order(incNY$thirdQ)])
p <- plot_ly(incNY, color = I("gray80")) %>%
add_segments(x = ~firstQ, xend = ~thirdQ, y = ~Industry, yend = ~Industry, name = "Middle 50%") %>%
#add_trace(x = ~exp(seq(1, 8)), name = "exponential") %>%
add_markers(x = ~low, y = ~Industry, name = "Low", color = I("Red")) %>%
add_markers(x = ~high, y = ~Industry, name = "High", color = I("Green")) %>%
add_markers(x = ~firstQ, y = ~Industry, name = "25th Percentile", color = I("orange")) %>%
add_markers(x = ~thirdQ, y = ~Industry, name = "75th Percentile", color = I("blue")) %>%
add_markers(x = ~median, y = ~Industry, name = "Median", color = I("black")) %>%
layout(
title = "Employees per Firm, by Industry",
xaxis = list(title = "Number of Employees per Firm, Logscale"),
margin = list(l = 200)
)
p <- layout(p, xaxis = list(type = "log"), yaxis = list(dtick = 1))
p
#Source: https://plot.ly/r/dumbbell-plots/
Now imagine you work for an investor and want to see which industries generate the most revenue per employee. Create a chart that makes this information clear. Once again, the distribution per industry should be shown.
library(plotly)
incRev <- inc %>%
filter(complete.cases(.)) %>%
group_by(Industry) %>%
mutate(RevenuePerEmp = round(Revenue/Employees/1000)) %>%
summarise(low = min(RevenuePerEmp), firstQ = quantile(RevenuePerEmp, 0.25),
median = median(RevenuePerEmp), thirdQ = quantile(RevenuePerEmp, 0.75),
high = max(RevenuePerEmp))
incRev$Industry <- factor(incRev$Industry, levels = incRev$Industry[order(incRev$thirdQ)])
p <- plot_ly(incRev, color = I("gray80")) %>%
add_segments(x = ~firstQ, xend = ~thirdQ, y = ~Industry, yend = ~Industry, name = "Middle 50%") %>%
#add_trace(x = ~exp(seq(1, 8)), name = "exponential") %>%
add_markers(x = ~low, y = ~Industry, name = "Low", color = I("Red")) %>%
add_markers(x = ~firstQ, y = ~Industry, name = "25th Percentile", color = I("orange")) %>%
add_markers(x = ~thirdQ, y = ~Industry, name = "75th Percentile", color = I("blue")) %>%
add_markers(x = ~high, y = ~Industry, name = "High", color = I("Green")) %>%
add_markers(x = ~median, y = ~Industry, name = "Median", color = I("black")) %>%
layout(
title = "Revenue Per Employee",
xaxis = list(title = "Annual $K Revenue per Employee, Logscale"),
margin = list(l = 250)
)
p <- layout(p, xaxis = list(type = "log"), yaxis = list(dtick = 1))
p
#Source: https://plot.ly/r/dumbbell-plots/